﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.LevelBusinessLogic;
using GovernmentOfficeProject.SysTools;

namespace GovernmentOfficeProject.LevelViewUI.admin
{
    public partial class UserManagerRelation : System.Web.UI.Page
    {
        //使用到的业务逻辑类:
        private static BLL_USER userBLL = new BLL_USER();
        private static BLL_Privilege priBLL = new BLL_Privilege();
        private static BLL_UserRelation uRelBLL = new BLL_UserRelation();


        //主控用户参数:用户当前在GridViewMaster的选择.
        private static decimal dclMastarUserID = 0;
        private static string strMasterUserLoginName = "";
        //受控用户参数:用户当前在GridViewControlled的选择.
        private static decimal dclControlledUserID = 0;
        private static string strControlledUserLoginName = "";

        //private decimal dclControlledPrivilegeID = 0;

        //GridView的绑定数据列表:
        private List<Entity_USER_User> userListByMaster = null;             //主控制用的所有用户列表.
        private List<Entity_USER_User> userListByControlled = null;         //受控制用的所有用户列表.
        private List<Entity_USER_User> userListByRelationIsMasterUser = null;   //受控制用的根据主控选择的用户获取对应的受控用户列表.
        
        //页面控件值记录:
        private static string strDDLControlledUserListSelect = "ALL";  //默认选中所有.

        //public static string strGlobDv1SP = "";
        
        //页面执行加载:
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0 || SystemContext.IsAdmin == 0)
            {
                Response.Redirect("/LevelViewUI/admin/Login.htm?FromUrl=" + HttpUtility.UrlEncode(Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;
                SystemContext.IsAdmin = 1;
            }
            if (!Page.IsPostBack)
            {
                //Session.Add("hiddgv1", "noValue");
                pageControlValueDefault();
                initGlobalUseValue();
                operationStateShow("页面加载完成,请继续操作.");
            }
            //if (this.hiddDivGv1ScollTop.Value != "")
            //{
            //    //string strScript = "document.getElementById('divUserGV1').scrollTop=document.getElementById('hiddDivGv1ScollTop').value;";
            //    //ScriptManager.RegisterStartupScript(this, this.GetType(), "setDV1Scoll", strScript.ToString(), true);
            //}
            //AppLog.Write(this.hiddDivGv1ScollTop.Value, AppLog.LogMessageType.Debug);
            ////AppLog.Write(ViewState["hiddDivGv1ScollTop"].ToString(), AppLog.LogMessageType.Debug);
        }

        //控件默认值:
        private void pageControlValueDefault()
        {
            this.txtMasterUserName.Text = "";
            this.txtControlledUserName.Text = "";
            this.txtRelationMasterUserName.Text = "";
            this.txtRelationControlledUserName.Text = "";
            this.txtRelationPrivilege.Text = "";
            //GridView默认显示所有用户.
            this.gvMasterUserDataBind();
            this.gvControlledUserDataBind(null);
        }

        //初始化当前页全局默认使用值:
        private bool initGlobalUseValue()
        {
            try
            {
                userListByMaster = userBLL.getAllUser();
                userListByControlled = userBLL.getAllUser();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }

        //主控和受控用户的参数默认值:
        private void defMCUserSelectValue()
        {
            //dclMastarUserID = 0;
            //strMasterUserLoginName = "";
            //dclControlledUserID = 0;
            //strControlledUserLoginName = "";
        }

        //显示操作状态:
        private void operationStateShow(string strInfo)
        {
            this.lblOperationShow.Text = "操作状态:";
            this.lblOperationShow.Text = this.lblOperationShow.Text + strInfo;
        }



        /*---------------------MasterUser操作区域的方法及事件:-------------------------*/
        //点击行选定,替代选择按钮.
        protected void GridViewMasterUser_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", "javascirpt:__doPostBack('GridViewMasterUser','Select$" + e.Row.RowIndex + "')");
                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }
        //绑定数据:给带条件的按钮"查找"用.
        private void gvMasterUserDataBind(object objDataSource)
        {
            GridViewMasterUser.DataSource = (List<Entity_USER_User>)objDataSource;
            GridViewMasterUser.DataBind();
        }
        //绑定数据给按钮"全部"和页面初始化使用.
        private void gvMasterUserDataBind()
        {
            if (userListByMaster != null)
            {
                gvMasterUserDataBind(userListByMaster);
            }
            else
            {
                gvMasterUserDataBind(new BLL_USER().getAllUser());
            }
        }
        //全部按钮:获取所有用户列表.
        protected void btnAllMasterUser_Click(object sender, EventArgs e)
        {
            gvMasterUserDataBind();
            //this.txtMasterUserName.Text = "";
            //defMCUserSelectValue();
            operationStateShow("加载全部[用户(谁)]的列表.");
        }
        //查找按钮:获取指定名字的用户列表.
        protected void btnSelectMasterUser_Click(object sender, EventArgs e)
        {
            if (this.txtMasterUserName.Text.Trim()!="")
            {
                Entity_USER_User userEntityParameter = new Entity_USER_User();
                userEntityParameter.LoginName = this.txtMasterUserName.Text.Trim();
                List<Entity_USER_User> userListByCondition = userBLL.getUserListByCondition(userEntityParameter);
                if (userListByCondition.Count >= 1)
                {
                    gvMasterUserDataBind(userListByCondition);
                    operationStateShow("已加载相关用户.");
                }
                else
                {
                    gvMasterUserDataBind(null);
                    operationStateShow("没有相关用户,请检查条件字符串.");
                }

            }
            else
            {
                gvMasterUserDataBind(null);
                operationStateShow("没有相关用户,请检查条件字符串.");
            }
            //defMCUserSelectValue();
        }
        //选定行后执行:
        protected void GridViewMasterUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            this.GridViewAccess.DataBind();
            this.txtRelationPrivilege.Text = "";
            this.txtRelationControlledUserName.Text = "";
            //设置全局选定值:
            dclMastarUserID = Convert.ToDecimal(GridViewMasterUser.SelectedDataKey["UserID"].ToString());
            strMasterUserLoginName = GridViewMasterUser.SelectedDataKey["LoginName"].ToString().Trim();
            this.txtRelationMasterUserName.Text = strMasterUserLoginName;
            if (ddlControlledUserListSelect.SelectedValue == "ALL")
            {
                gvControlledUserDataBind(new BLL_USER().getAllUser());
            }
            if (strDDLControlledUserListSelect == "REL" || ddlControlledUserListSelect.SelectedValue=="REL")
            {
                //取得所有的对应用户,并重新赋值给GridViewControlled:
                List<Entity_USER_Relation> relationListIsMaster = uRelBLL.getRelationListByMasterUserID(dclMastarUserID);
                if (relationListIsMaster.Count == 0)
                {
                    gvControlledUserDataBind(null);
                    operationStateShow("[" + strMasterUserLoginName + "]没有关联权限关系的用户.");
                }
                else
                {
                    List<Entity_USER_User> userList = new List<Entity_USER_User>();
                    foreach (Entity_USER_Relation rel in relationListIsMaster)
                    {
                        userList.Add(userBLL.getUserByUserID(rel.UrControlledUserID));
                    }
                    //去掉userList中的重复对象:
                    List<Entity_USER_User> userListProcessed = userList.Distinct(new Entity_USER_User_Comparer()).ToList<Entity_USER_User>();
                    gvControlledUserDataBind(userListProcessed);
                    operationStateShow("已加载关联用户列表.共获取关联用户" + userListProcessed.Count.ToString() + "位.");
                }
            }
        }


        /*---------------------ControlledUser操作区域的方法及事件:-------------------------*/
        //下拉选择"用户(具有)"的用户列表:是所有用户,还是根据选择"用户(谁)"指定的用户的有关系"用户(具有)"列表:
        protected void ddlControlledUserListSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlControlledUserListSelect.SelectedValue == "ALL")     //显示所有.
            {
                strDDLControlledUserListSelect = "ALL";
                if (dclMastarUserID == 0 && strMasterUserLoginName == "")
                {
                    gvControlledUserDataBind(null);
                    operationStateShow("请先选择[用户(谁)]中的用户.");
                }
                else
                {
                    this.gvControlledUserDataBind();
                    operationStateShow("ALL");
                }
            }
            if (this.ddlControlledUserListSelect.SelectedValue == "REL")    //显示指定关系.
            {
                strDDLControlledUserListSelect = "REL";
                if (dclMastarUserID == 0 && strMasterUserLoginName == "") //没有选择[用户(谁)],前提必须先选择[用户(谁)]获取userID,根据userID获取对应授权关系的[用户(具有)].
                {
                    gvControlledUserDataBind(null);
                    operationStateShow("请先选择[用户(谁)]中的用户.");
                }
                else
                {
                    //获取指定关系列表业务操作逻辑代码:
                    //根据ID获取所有:
                    //取得所有的对应用户,并重新赋值给GridViewControlled:
                    List<Entity_USER_Relation> relationListIsMaster = uRelBLL.getRelationListByMasterUserID(dclMastarUserID);
                    //AppLog.Write("247relationListIsMaster-Count:" + relationListIsMaster.Count.ToString(), AppLog.LogMessageType.Debug);
                    if (relationListIsMaster.Count == 0)
                    {
                        gvControlledUserDataBind(null);
                        operationStateShow("[" + strMasterUserLoginName + "]没有关联权限关系的用户.");
                    }
                    else
                    {
                        List<Entity_USER_User> userList = new List<Entity_USER_User>();
                        foreach (Entity_USER_Relation rel in relationListIsMaster)
                        {
                            userList.Add(userBLL.getUserByUserID(rel.UrControlledUserID));
                        }
                        //去掉userList中的重复对象:
                        List<Entity_USER_User> userListProcessed = userList.Distinct(new Entity_USER_User_Comparer()).ToList<Entity_USER_User>();
                        gvControlledUserDataBind(userListProcessed);
                        operationStateShow("已加载关联用户列表.共获取关联用户" + userListProcessed.Count.ToString() + "位.");
                    }
                }
            }
        }
        //点击行选定,替代选择按钮.
        protected void GridViewControlledUser_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", "javascirpt:__doPostBack('GridViewControlledUser','Select$" + e.Row.RowIndex + "')");
                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }
        //绑定数据:给带条件的按钮"查找"用.
        private void gvControlledUserDataBind(object objDataSource)
        {
            GridViewControlledUser.DataSource = (List<Entity_USER_User>)objDataSource;
            GridViewControlledUser.DataBind();
        }
        //绑定数据给按钮"全部"和页面初始化使用.
        private void gvControlledUserDataBind()
        {
            if (userListByControlled != null)
            {
                gvControlledUserDataBind(userListByControlled);
            }
            else
            {
                gvControlledUserDataBind(new BLL_USER().getAllUser());
            }
        }
        //全部按钮:获取所有用户列表.
        protected void btnAllControlledUser_Click(object sender, EventArgs e)
        {
            //this.txtControlledUserName.Text = "";
            if (dclMastarUserID == 0 && strMasterUserLoginName == "")
            {
                gvControlledUserDataBind(null);
                operationStateShow("请先选择[用户(谁)]中的用户.");
            }
            else
            {
                this.ddlControlledUserListSelect.SelectedValue = "ALL";
                gvControlledUserDataBind();
            }
            //defMCUserSelectValue();
        }
        //查找按钮:获取指定名字的用户列表.
        protected void btnSelectControlled_Click(object sender, EventArgs e)
        {
            if (dclMastarUserID == 0 && strMasterUserLoginName == "")
            {
                gvControlledUserDataBind(null);
                operationStateShow("请先选择[用户(谁)]中的用户.");
            }
            else
            {
                this.ddlControlledUserListSelect.SelectedValue = "ALL";
                if (this.txtControlledUserName.Text.Trim() != "")
                {
                    Entity_USER_User userEntityParameter = new Entity_USER_User();
                    userEntityParameter.LoginName = this.txtControlledUserName.Text.Trim();
                    List<Entity_USER_User> userListByCondition = userBLL.getUserListByCondition(userEntityParameter);
                    if (userListByCondition.Count >= 1)
                    {
                        gvControlledUserDataBind(userListByCondition);
                        operationStateShow("已加载相关用户.");
                    }
                    else
                    {
                        gvControlledUserDataBind(null);
                        operationStateShow("没有相关用户,请检查条件字符串.");
                    }
                }
                else
                {
                    gvControlledUserDataBind(null);
                    operationStateShow("没有相关用户,请检查条件字符串.");
                }
            }           
            //defMCUserSelectValue();
        }
        //选定行后执行:
        protected void GridViewControlledUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtRelationPrivilege.Text = "";           
            if (dclMastarUserID==0 && strMasterUserLoginName=="") //没有选择[用户(谁)],前提必须先选择[用户(谁)]获取userID,再继续选择[用户(具有)]的userID,进行关系授权.
            {
                gvControlledUserDataBind(null);
                operationStateShow("请先选择[用户(谁)]中的用户.");
            }
            else
            {
                //设置全局选定值:
                dclControlledUserID = Convert.ToDecimal(GridViewControlledUser.SelectedDataKey["UserID"].ToString());
                strControlledUserLoginName = GridViewControlledUser.SelectedDataKey["LoginName"].ToString().Trim();
                this.txtRelationControlledUserName.Text = strControlledUserLoginName;
                //operationStateShow(dclControlledUserID + "-" + strControlledUserLoginName);
                //添加关系授权GridView的List绑定代码:
                if (dclControlledUserID == dclMastarUserID)
                {
                    operationStateShow("ID不能相同");
                    this.GridViewAccess.DataSource = null;
                    this.GridViewAccess.DataBind();
                }
                else
                {
                    this.GridViewAccess.DataSource = getMenuAndButtonDefaultList();
                    this.GridViewAccess.DataBind();
                }
            }
        }

        /*---------------------RelationOperation操作区域的方法及事件:-------------------------*/
        //获取授权默认绑定显示列表(不包含选定):
        private List<Entity_SYS_MenuAndButtonCollection> getMenuAndButtonDefaultList()
        {
            BLL_SYSTEM systemBLL = new BLL_SYSTEM();
            List<Entity_SYS_MenuAndButtonCollection> mbList = new List<Entity_SYS_MenuAndButtonCollection>();
            List<Entity_SYS_Menu> mList = new BLL_SYSTEM().getAllMenu();
            List<Entity_SYS_Button> bList = new BLL_SYSTEM().getAllButton();
            List<Entity_SYS_Button> bListByMenuNo = null;
            int iOrder = 1;
            foreach (Entity_SYS_Menu menuOne in mList)
            {
                if (menuOne.MenuNo == "RiChengManager" || menuOne.MenuNo == "GeRenRCManager")   //限制为只取日程的个人日程模块,用在GridViewAccess显示.
                {
                    if (menuOne.MenuParentNo == "")
                    {
                        Entity_SYS_MenuAndButtonCollection mbEntity = new Entity_SYS_MenuAndButtonCollection();
                        mbEntity.OrderID = iOrder;
                        mbEntity.MenuID = menuOne.MenuID;
                        mbEntity.MenuName = menuOne.MenuName;
                        mbEntity.MenuNo = menuOne.MenuNo;
                        mbEntity.MenuParentNo = menuOne.MenuParentNo;
                        mbList.Add(mbEntity);
                        iOrder++;
                    }
                    //根据当前menuNo获取所有button
                    Entity_SYS_Button btnEntity = new Entity_SYS_Button();
                    btnEntity.BtnMenuNo = menuOne.MenuNo;
                    bListByMenuNo = systemBLL.getButtonListByCondition(btnEntity);
                    if (!bListByMenuNo.Count.Equals(0))
                    {
                        foreach (Entity_SYS_Button btnOne in bListByMenuNo)
                        {
                            //if (btnOne.BtnMenuNo=="GeRenRCManager")
                            //{
                            Entity_SYS_MenuAndButtonCollection mbEntity = new Entity_SYS_MenuAndButtonCollection();
                            mbEntity.OrderID = iOrder;
                            mbEntity.MenuID = menuOne.MenuID;
                            mbEntity.MenuName = menuOne.MenuName;
                            mbEntity.MenuNo = menuOne.MenuNo;
                            mbEntity.MenuParentNo = menuOne.MenuParentNo;
                            mbEntity.ButtonId = btnOne.BtnID;
                            mbEntity.ButtonName = btnOne.BtnName;
                            mbEntity.ButtonNo = btnOne.BtnNo;
                            mbList.Add(mbEntity);
                            iOrder++;
                            //}
                        }
                    }
                }
            }
            return mbList;
        }
        //行绑定数据时候判断授权状态并赋值用于显示.
        protected void GridViewAccess_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //先根据Relation获取masterUser对应的controlledUser列表(包含mid,cid,privilegeid),
            //根据获取的privilegeid授权选中,其他为禁止,
            List<Entity_USER_Relation> relList = uRelBLL.getRelationListByMasterUIDAndControlledUID(dclMastarUserID, dclControlledUserID);
            List<Entity_SYS_Privilege> priList = new List<Entity_SYS_Privilege>();
            foreach (Entity_USER_Relation rel in relList)
            {
                priList.Add(priBLL.getPrivilegeByPrivilegeID(rel.UrControlledPrivilegeID));
            }
            if (priList.Count==0)
            {
                operationStateShow("用户[" + strMasterUserLoginName + "]与用户[" + strControlledUserLoginName + "]没有关联授权.");
            }
            else
            {
                operationStateShow("用户[" + strMasterUserLoginName + "]具有用户[" + strControlledUserLoginName + "]的[" + priList.Count.ToString() + "]项授权.");
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                /*取当前行的值:*/
                string strMenuName = DataBinder.Eval(e.Row.DataItem, "menuName").ToString();
                string strButtonName = DataBinder.Eval(e.Row.DataItem, "buttonName").ToString();
                decimal dclMenuID = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "menuID"));
                string strMenuNo = DataBinder.Eval(e.Row.DataItem, "menuNo").ToString();
                string strMenuParentNo = DataBinder.Eval(e.Row.DataItem, "menuParentNo").ToString();
                decimal dclButtonID = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "buttonID"));
                decimal dclOrderID = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "orderID"));
                if (strButtonName == "" && dclButtonID == 0)  //没有功能按钮值并且button为默认值0(未赋值)的是模块的顶级.
                {
                    ((CheckBox)e.Row.FindControl("checkBoxSelect")).Visible = false;
                    e.Row.BackColor = System.Drawing.Color.YellowGreen;
                }
                //AppLog.Write("")
                //if (priList.Count > 0)
                //{
                    foreach (Entity_SYS_Privilege privilege in priList)
                    {
                        /*获取menu、获取button、判断对应类型，判断是否授权，显示checkbox：*/
                        Entity_SYS_Menu menu = null;
                        Entity_SYS_Button button = null;
                        if (dclMenuID != 0)
                        {
                            menu = new BLL_SYSTEM().getMenuByMenuID(dclMenuID);
                        }
                        if (dclButtonID != 0)
                        {
                            button = new BLL_SYSTEM().getButtonByButtonID(dclButtonID);
                        }
                        if (strMenuName != "" && strButtonName != "" && dclButtonID != 0)
                        {
                            //if (privilege.PriID > 0)
                            //{
                            if (privilege.PriOperation == "Permit" && privilege.PriAccess == "SYS_Button" && privilege.PriAccessValue == button.BtnID)
                            {
                                ((CheckBox)e.Row.FindControl("checkBoxSelect")).Checked = true;
                            }
                            //}
                        }
                    }
                //}
            }          
        }

        protected void CheckBoxSelect_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow gdr = (GridViewRow)((CheckBox)sender).Parent.Parent;
            /*取当前行的值:string若为空=="&nbsp"*/
            string strMenuName = "";
            if (gdr.Cells[1].Text.Trim().Equals("&nbsp"))
            { strMenuName = ""; }
            else
            { strMenuName = gdr.Cells[1].Text.Trim(); }
            string strButtonName = "";
            if (gdr.Cells[2].Text.Trim().Equals("&nbsp"))
            { strButtonName = ""; }
            else
            { strButtonName = gdr.Cells[2].Text.Trim(); }
            decimal dclMenuID = Convert.ToDecimal(gdr.Cells[4].Text.Trim());
            string strMenuNo = gdr.Cells[5].Text.Trim();
            string strMenuParentNo = gdr.Cells[6].Text.Trim();
            decimal dclButtonID = Convert.ToDecimal(gdr.Cells[3].Text.Trim());
            decimal dclOrderID = Convert.ToDecimal(gdr.Cells[7].Text.Trim());

            if (((CheckBox)sender).Checked)
            {
                //先判断下用户(谁)有没有领导查看的权限,没有不能进行授权操作.
                //根据MasterUID查询对应领导日程操作Button权限,

                if (strMenuName != "" && strButtonName != "" && dclButtonID != 0)   //确定当前行是button行.
                {
                    bool blRolePrivilege = false;
                    bool blUserPrivilege = false;
                    //获取用户ID对应的ROLE表的roleID
                    //判断roleID在privilege表的数据
                    List<Entity_USER_Role> roleListByUID = new BLL_RoleManager().getRoleListByUserID(dclControlledUserID);
                    if (roleListByUID.Count > 0)
                    {
                        foreach (Entity_USER_Role rol in roleListByUID)
                        {
                            Entity_SYS_Privilege privilegeRoleEntityParameter = new Entity_SYS_Privilege();
                            privilegeRoleEntityParameter.PriMaster = "PER_Role";
                            privilegeRoleEntityParameter.PriMasterValue = rol.RoleID;
                            privilegeRoleEntityParameter.PriAccess = "SYS_Button";
                            privilegeRoleEntityParameter.PriAccessValue = dclButtonID;
                            Entity_SYS_Privilege rolePrivilege = new BLL_Privilege().getPrivilegeByCondition(privilegeRoleEntityParameter);
                            if (rolePrivilege.PriID > 0)
                            {
                                Entity_USER_Relation relationEntityParameter = new Entity_USER_Relation();
                                relationEntityParameter.UrMasterUserID = dclMastarUserID;
                                relationEntityParameter.UrControlledUserID = dclControlledUserID;
                                relationEntityParameter.UrControlledPrivilegeID = rolePrivilege.PriID;
                                bool flag = uRelBLL.addRelation(relationEntityParameter);
                                if (flag)
                                {
                                    if (blRolePrivilege != true)
                                    {
                                        blRolePrivilege = true;
                                    }
                                }
                            }
                        }
                    }
                    //判断USER在privilege表的数据
                    Entity_SYS_Privilege privilegeUserEntityParameter = new Entity_SYS_Privilege();
                    privilegeUserEntityParameter.PriMaster = "PER_User";
                    privilegeUserEntityParameter.PriMasterValue = dclControlledUserID;
                    privilegeUserEntityParameter.PriAccess = "SYS_Button";
                    privilegeUserEntityParameter.PriAccessValue = dclButtonID;
                    Entity_SYS_Privilege userPrivilege = new BLL_Privilege().getPrivilegeByCondition(privilegeUserEntityParameter);
                    if (userPrivilege.PriID > 0)
                    {
                        //不为空则判断这条private数据是否为permit:为primit则ADD关系;为forbid则提示.
                        if (userPrivilege.PriOperation == "Permit")
                        {
                            Entity_USER_Relation relationEntityParameter = new Entity_USER_Relation();
                            relationEntityParameter.UrMasterUserID = dclMastarUserID;
                            relationEntityParameter.UrControlledUserID = dclControlledUserID;
                            relationEntityParameter.UrControlledPrivilegeID = userPrivilege.PriID;
                            bool flag = uRelBLL.addRelation(relationEntityParameter);
                            if (flag)
                            {
                                blUserPrivilege = true;
                            }
                        }
                        //if (userPrivilege.PriOperation == "Forbid")
                        //{
                        //    ((CheckBox)sender).Checked = false;
                        //    operationStateShow("用户[" + strControlledUserLoginName + "]选定的权限已被禁止,用户[" + strMasterUserLoginName + "]无法关联用户[" + strControlledUserLoginName + "]的[" + strMenuName + "-" + strButtonName + "]权限");
                        //}
                    }
                    if (blRolePrivilege==false && blUserPrivilege == false)    //根据条件没取得数据,说明private表没有该条button数据,.
                    {
                        ((CheckBox)sender).Checked = false;
                        operationStateShow("用户[" + strControlledUserLoginName + "]未被授予选定的权限,用户[" + strMasterUserLoginName + "]无法关联用户[" + strControlledUserLoginName + "]的[" + strMenuName + "-" + strButtonName + "]权限");
                    }
                    if (blRolePrivilege == true || blUserPrivilege == true)
                    {
                        operationStateShow("↓↓↓已授权关联权限↓↓↓");
                        this.txtRelationMasterUserName.Text = strMasterUserLoginName;
                        this.txtRelationControlledUserName.Text = strControlledUserLoginName;
                        this.txtRelationPrivilege.Text = strMenuName + "-" + strButtonName;
                    }
                }
            }
            else //取消授权.
            {
                bool blRolePrivilege = false;
                bool blUserPrivilege = false;
                List<Entity_USER_Role> roleListByUID = new BLL_RoleManager().getRoleListByUserID(dclControlledUserID);
                if (roleListByUID.Count > 0)
                {
                    foreach (Entity_USER_Role rol in roleListByUID)
                    {
                        Entity_SYS_Privilege privilegeRoleEntityParameter = new Entity_SYS_Privilege();
                        privilegeRoleEntityParameter.PriMaster = "PER_Role";
                        privilegeRoleEntityParameter.PriMasterValue = rol.RoleID;
                        privilegeRoleEntityParameter.PriAccess = "SYS_Button";
                        privilegeRoleEntityParameter.PriAccessValue = dclButtonID;
                        Entity_SYS_Privilege rolePrivilege = new BLL_Privilege().getPrivilegeByCondition(privilegeRoleEntityParameter);
                        if (rolePrivilege.PriID > 0)
                        {
                            Entity_USER_Relation relationEntity = uRelBLL.getRelationEntityByMasterUIDAndControlledUIDAndPrivilegeID(dclMastarUserID, dclControlledUserID, rolePrivilege.PriID);
                            bool flag = uRelBLL.deleteRelationByRelationID(relationEntity.UrID);
                            if (flag)
                            {
                                if (blRolePrivilege != true)
                                {
                                    blRolePrivilege = true;
                                }
                            }
                        }
                    }
                }
                Entity_SYS_Privilege privilegeUserEntityParameter = new Entity_SYS_Privilege();
                privilegeUserEntityParameter.PriMaster = "PER_User";
                privilegeUserEntityParameter.PriMasterValue = dclControlledUserID;
                privilegeUserEntityParameter.PriAccess = "SYS_Button";
                privilegeUserEntityParameter.PriAccessValue = dclButtonID;
                Entity_SYS_Privilege userPrivilege = new BLL_Privilege().getPrivilegeByCondition(privilegeUserEntityParameter);
                if (userPrivilege.PriID > 0)
                {
                    Entity_USER_Relation relationEntity = uRelBLL.getRelationEntityByMasterUIDAndControlledUIDAndPrivilegeID(dclMastarUserID, dclControlledUserID, userPrivilege.PriID);
                    bool flag = uRelBLL.deleteRelationByRelationID(relationEntity.UrID);
                    if (flag)
                    {
                        blUserPrivilege = true;
                    }
                }
                if (blRolePrivilege == true || blUserPrivilege == true)
                {
                    operationStateShow("↓已禁止关联权限↓");
                    this.txtRelationMasterUserName.Text = strMasterUserLoginName;
                    this.txtRelationControlledUserName.Text = strControlledUserLoginName;
                    this.txtRelationPrivilege.Text = strMenuName + "-" + strButtonName;
                }

                ///////////////
                //Entity_SYS_Privilege privilegeEntityParameter = new Entity_SYS_Privilege();
                //privilegeEntityParameter.PriMaster = "PER_User";
                //privilegeEntityParameter.PriMasterValue = dclControlledUserID;
                //privilegeEntityParameter.PriAccess = "SYS_Button";
                //privilegeEntityParameter.PriAccessValue = dclButtonID;
                //Entity_SYS_Privilege privilege = new BLL_Privilege().getPrivilegeByCondition(privilegeEntityParameter);

                //Entity_USER_Relation relationEntityParameter = new Entity_USER_Relation();
                //relationEntityParameter.UrMasterUserID = dclMastarUserID;
                //relationEntityParameter.UrControlledUserID = dclControlledUserID;
                //relationEntityParameter.UrControlledPrivilegeID = privilege.PriID;

                //decimal dclDeleteRelationID = uRelBLL.getRelationEntityByMasterUIDAndControlledUIDAndPrivilegeID(dclMastarUserID, dclControlledUserID, privilege.PriID).UrID;
                //bool flag = uRelBLL.deleteRelationByRelationID(dclDeleteRelationID);
                //if (flag)
                //{
                //    operationStateShow("↓已禁止关联权限↓");
                //    this.txtRelationMasterUserName.Text = strMasterUserLoginName;
                //    this.txtRelationControlledUserName.Text = strControlledUserLoginName;
                //    this.txtRelationPrivilege.Text = strMenuName + "-" + strButtonName;
                //    //operationStateShow("已禁止[" + new BLL_USER().getUserByUser(user).LoginName + "]用户-[" + strMenuName + "]-[" + strButtonName + "]的功能.");
                //}                
            }
        }

    }
}
